<%-- 
    Document   : publish
    Created on : Oct 15, 2009, 6:10:57 PM
    Author     : balbir kaur
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s"  uri="/struts-tags"%>
<%@taglib prefix="ud" uri="userdata"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="../include/quiz.css" rel="stylesheet" media="screen"  type="text/css"/>
        <title>Publish Results</title>
    </head>
    <body>

        <%@ page import="org.netbeans.saas.*, org.netbeans.saas.facebook.*,json.*,java.util.*" %>

        <s:include value="../include/header.html"/>
        <div id="allcontent">
            <div id="leftsidebar">
                <ul><li><em><a href="<s:url value='show.action'/>"> Take Quiz Again </a></em></li></ul><br/>
                <script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US"></script><script type="text/javascript">FB.init("d7780c59853dfbf50ca590bc0dc31017");</script><fb:fan profile_id="192745930800" stream="1" connections="10" height="120" width="150"></fb:fan><div style="font-size:8px; padding-left:10px"><a href="http://www.facebook.com/apps/application.php?id=192745930800">cs1.6quiz on Facebook</a> </div>
            </div>
            <div id="main"> 

                <%
            Long uid = null;
            try {
                String format1 = null;

                RestResponse result2 = FacebookSocialNetworkingService.usersGetLoggedInUser(request, response, format1);
                if (result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse) {
                    facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse result2Obj = result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse.class);
                    uid = result2Obj.getValue();
                } else if (result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.ErrorResponse) {
                    facebook.socialnetworkingservice.facebookresponse.ErrorResponse result2Obj = result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class);
                }
                //TODO - Uncomment the print Statement below to print result.
                //out.println("The SaasService returned: "+result2.getDataAsString());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            session.setAttribute("userid", uid);
                %>



                <s:if test="%{#session.userid!=null}">


                    <%
            ArrayList<Long> al = null;
            try {
                String format1 = null;
                String flid = null;

                RestResponse result2 = FacebookSocialNetworkingService.friendsGet(request, response, format1, flid);
                if (result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse) {
                    facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse result2Obj = result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse.class);
                    al = new ArrayList<Long>(result2Obj.getUid());
                } else if (result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.ErrorResponse) {
                    facebook.socialnetworkingservice.facebookresponse.ErrorResponse result2Obj = result2.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class);
                }
                //TODO - Uncomment the print Statement below to print result.
                //out.println("The SaasService returned: "+result2.getDataAsString());
                } catch (Exception ex) {
                ex.printStackTrace();
            }
            session.setAttribute("friends", al);
                    %>

                </s:if>

                <s:set name="message" value="%{message}" scope="request"/>
                <s:set name="percentage" value="%{percentage}" scope="request"/>

                <ud:userposition appUsers="${friends}" uid="${userid}" percentage="${percentage}"/>

                <%
            String rank = (String) pageContext.getAttribute("userrank");%>
                <%
            if (rank != null) {

                try {
                    String message = (String) session.getAttribute("message");
                    String templateBundleId = "197776770800";
                    String targetIds = null;
                    JSONObject obj = new JSONObject();
                    String bodyGeneral = null;
                    String storySize = "2";
                    StringBuffer buffer = new StringBuffer();
                    obj.put("message", "and scored");
                    obj.put("score", message);
                    obj.put("beat", rank);
                    JSONObject obj1 = new JSONObject();

                    obj1.put("src", "http://imgcash2.imageshack.us/img101/171/csv2.jpg");
                    obj1.put("href", "http://ramannanda.blogdns.com:4000/cs1.6quiz");
                    obj.accumulate("images", obj1);
                    obj1.put("src", "http://imgcash3.imageshack.us/img224/7821/mave.jpg");
                    obj1.put("href", "http://ramannanda.blogdns.com:4000/cs1.6quiz");
                    obj.accumulate("images", obj1);
                    obj1.put("src", "http://imgcash2.imageshack.us/img48/3504/csv1.jpg");
                    obj1.put("href", "http://ramannanda.blogdns.com:4000/cs1.6quiz");
                    obj.accumulate("images", obj1);
                    buffer.append(obj.toString());
                    String templateData = buffer.toString();
                    RestResponse result = FacebookSocialNetworkingService.feedPublishUserAction(request, response, templateBundleId, templateData, targetIds, bodyGeneral, storySize);
                    if (result.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.FeedPublishUserActionResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.FeedPublishUserActionResponse) {
                        facebook.socialnetworkingservice.facebookresponse.FeedPublishUserActionResponse resultObj = result.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.FeedPublishUserActionResponse.class);
                    } else if (result.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.ErrorResponse) {
                        facebook.socialnetworkingservice.facebookresponse.ErrorResponse resultObj = result.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class);
                    }

                    //TODO - Uncomment the print Statement below to print result.
                    if (result.getDataAsString().equals("1")) {
                        out.println("The Result was posted sucessfully");
                    } else {
                        out.println("The Facebook service returned an error ");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();

                }

            }
            boolean flag = true;
            request.setAttribute("flag", flag);
                %>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script><script type="text/javascript">FB.init("d7780c59853dfbf50ca590bc0dc31017");</script>
<% StringBuffer message=new StringBuffer();
                           message.append("I took the Counter Strike 1.6 quiz and scored ");
                           message.append((String)(session.getAttribute("message")));
                           String temp=(String) pageContext.getAttribute("userrank");
                           if(temp!=null &&temp!="NaN"){
                               message.append(" and beat ");
                               message.append(temp);
                               message.append(" of his friends");
                           }
                           String msg=message.toString();
                 

                    %>
<s:if test="%{flag==false}">
                    The application was unable to post the data due to error!<br/>
                    You may try the following link which will work!<br/><br/>
                    <script type="text/javascript">
                        function callPublish(msg, attachment, action_link) {
                            FB.ensureInit(function () {
                                FB.Connect.streamPublish(msg, attachment, action_link);
                            });
                        }</script>
                    
                    <input type="button" onclick="callPublish('<%=msg%>',{'name':'CS1.6QUIZ','href':'http://ramannanda.blogdns.com:4000/cs1.6quiz','description':'Take the quiz and find out whether you can beat him','media':[{'type':'image','src':'http://imgcash3.imageshack.us/img224/7821/mave.jpg','href':'http://ramannanda.blogdns.com:4000/cs1.6quiz'}]},[{'text':'Take Quiz','href':'http://ramannanda.blogdns.com:4000/cs1.6quiz'}]);return false;" value="Publish To facebook" />
                </s:if>
                <s:else>
                    <br/>
                    You may also want to write something <br/>
                    <script type="text/javascript">
                        function callPublish(msg, attachment, action_link) {
                            FB.ensureInit(function () {
                                FB.Connect.streamPublish(msg, attachment, action_link);
                            });
                        }</script>
                    
                    <input type="button" onclick="callPublish('',{'name':'CS1.6QUIZ','href':'http://ramannanda.blogdns.com:4000/cs1.6quiz','description':'<%=msg +"Take the quiz and find out whether you can beat him"%>','media':[{'type':'image','src':'http://imgcash3.imageshack.us/img224/7821/mave.jpg','href':'http://ramannanda.blogdns.com:4000/cs1.6quiz'}]},[{'text':'Take Quiz','href':'http://ramannanda.blogdns.com:4000/cs1.6quiz'}]);return false;" value="Publish To facebook" />
                

                </s:else>
            </div>
        </div>
        <s:include value="../include/footer.jsp"/>

    </body>
</html>
